package demo;

import entity.Person;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Demo7 {
    public static void main(String[] args) throws Exception {
        //获取class对象
        Class clz=Person.class;

        //1.创建person对象
        Object person=clz.newInstance();//其实就是调用无参构造
        System.out.println(person);

        //2.使用属性
        //2.1 给属性赋值
        Field field=clz.getField("age");
        field.set(person,18);

        Field field2=clz.getDeclaredField("name");
        field2.setAccessible(true);
        field2.set(person,"大俊");

        //2.2取出属性的值
        System.out.println(field.get(person));
        System.out.println(field2.get(person));

        //3.调用方法
        Method method= clz.getMethod("study",String.class);
        method.invoke(person,"java");

    }
}
